import wx from 'labrador';
import { create } from 'al-request';
import _ from 'lodash';
import { select } from 'redux-saga/effects';
import { generateUUID } from '../utils/utils';

export const MEDICAL_URL = 'https://images.cookmami.com';
// export const DEFAULT_REQUEST_URL = 'https://demo.cookmami.com/';
export const DEFAULT_REQUEST_URL = 'https://www.cookmami.com/';

export const REQUEST_LOGOUT = 'user/loginOut';
export const REQUEST_LOGIN = 'user/login';
export const REQUEST_SESSION = 'api/get_session';
export const REQUEST_POSTLIST = 'post';
export const REQUEST_DETAIL = 'post/detail';
export const REQUEST_COMMENTARTICLE = 'user/commentArticle';
export const REQUEST_POSTCOMMENT = 'post/comment';
export const REQUEST_CATE = 'good/cates';
export const REQUEST_PRODUCTS = 'good';
export const REQUEST_PRODUCT_RESOURCE = 'good/media';
export const REQUEST_UPLOAD_FORMID = 'user/uploadFormId';

export function createRequest(url) {
    return create({
        apiRoot: url || DEFAULT_REQUEST_URL,
        defaultHeader: {
            platform: 'cookmami-wx-assistant',
        }
    });
}

export function* getDefaultRequestParams() {
    const user = yield select(x => x.user);
    if (user) {
        return {
            uid: user.uid || '',
            deviceId: user.uuid || generateUUID(),
            token: user.token || '',
            openid: user.openid,
            systemType: user.systemType || '',
            appVersion: user.appVersion || '',
            deviceName: user.deviceName || '',
            operator: user.operator || '',
            systemVersion: user.systemVersion || '',
            connectionType: user.connectionType || '',
            deviceModel: user.deviceModel || ''
        }
    }
    return {
        deviceId: generateUUID()
    }
}

export function fetchPostListData(params) {
    let request = createRequest();
    return request.get(REQUEST_POSTLIST, {
        ...params,
        tag: 'assistant'
    });
}

export function* get(url, params) {
    wx.showLoading({
        title: '加载中...'
    })
    const defaultParams = yield getDefaultRequestParams();

    let request = createRequest();
    const res = yield request.get(url, {...params, ...defaultParams });
    wx.hideLoading();
    if (res.status === 1 || res.status === 10010) {
        return res.data;
    }
    return undefined;
}

export function* post(url, params) {
    wx.showLoading({
        title: '加载中...'
    })
    const defaultParams = yield getDefaultRequestParams();
    let request = createRequest();
    const res = yield request.post(url, {...params, ...defaultParams }, {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    });
    wx.hideLoading();
    if (res.status === 10010) {
        return res.data;
    } else if (res.status == 10001) {
        wx.navigateTo({ url: '/pages/login/index' });
        // wx.showToast({ title: response.message });
    }
    return undefined;
}

export function* commentPost(url, params) {
    wx.showLoading({
        title: '加载中...'
    })
    const defaultParams = yield getDefaultRequestParams();
    let request = createRequest();
    const res = yield request.post(url, {...params, ...defaultParams }, {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    });
    wx.hideLoading();

    if (res.status === 10010) {
        return res.data;
    } else if (res.status == 10001) {
        wx.navigateTo({ url: '/pages/login/index' });
        // wx.showToast({ title: response.message });
    } else if (res.status == 0) {
        wx.showToast({
            title: '评论失败',
            duration: 1000
        });
        return undefined;
    }
    return undefined;
}

export function fetchCookList(params) {
    let request = createRequest();
    return request.get(REQUEST_MYCOOK, {
        ...params,
        appVersion: '00.03.80'
    });
}

export function fetchSaveBoxList(params) {
    let request = createRequest();
    return request.get(REQUEST_GetCookDraftList, params);
}

export function UpLoadSaveBoxItem(params) {
    let request = createRequest();
    return request.post(REQUEST_UpLoadCookDraftList,
        params, {
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        });
}

export function deleteCook(params) {
    let request = createRequest();
    return request.get(REQUESR_DELETECOOK, {
        ...params,
        appVersion: '2.2.0'
    });
}
export function downloadImg(params) {
    let request = createRequest();
    return request.get(REQUEST_DOWNLOAD, {
        ...params,
        appVersion: '2.2.0'
    });
}
export function publishCook(path, params, callback, self) {
    wx.uploadFile({
        url: DEFAULT_REQUEST_URL + REQUEST_PUBLISH,
        filePath: path,
        name: 'cover',
        formData: params,
        success: function(res) {
            callback(res, self, params);
        }
    });
}
//export function publishCook(params) {
//  let request = createRequest();
//  return request.get(REQUEST_PUBLISH, {
//    ...params,
//    appVersion: '2.2.0'
//  });
//}
export function search(params) {
    let request = createRequest();
    return request.get(REQUEST_SEARCH, {
        pageNum: params.pageNum || 0,
        pageSize: 10,
        keyword: params.keyword,
        type: 'cook',
        age: params.age,
        field: params.field,
        order: params.order,
        is_cloud: 0,
        appVer: '2.2.0'
    });
}
export function getList(params) {
    let request = createRequest();

    let data = {
        pageNum: params.pageNum || 0,
        pageSize: 10,
        source: 'weixinApp',
        appVer: '2.2.0'
    };

    if (_.has(params, 'is_jingxuan')) {
        data.is_jingxuan = params.is_jingxuan;
    }

    if (_.has(params, 'tag')) {
        data.tag = params.tag;
    }

    if (_.has(params, 'age') && params.age > 0) {
        data.age = params.age;
    }

    if (_.has(params, 'field')) {
        data.field = params.field;
    }

    return request.get(REQUEST_COOK, data);
}

export function getDetail(params) {
    let request = createRequest();
    return request.get(REQUEST_DETAILS, {
        cook_id: params.id,
        login_uid: params.uid,
        appVer: '2.2.0'
    });
}

export function getConfig() {
    let request = createRequest();
    return request.get(REQUEST_INIT_CONFIG, {
        source: 'weixinapp',
        appVer: '2.2.0'
    });
}

export function getComment(params) {
    let request = createRequest();
    return request.get(REQUEST_COOK_COMMENT, {
        id: params.id,
        pageNum: params.pageNum || 0,
        pageSize: 10,
        appVer: '2.2.0'
    });
}

export function getCode(params) {
    let request = createRequest();
    return request.get(REQUEST_USER_SUMSEND, {
        mobile: params.mobile
    });
}

export function login(params) {
    let request = createRequest();
    return request.post(
        REQUEST_LOGIN, {
            type: params.type || 'mobile',
            openId: params.mobile || params.openid,
            deviceId: params.uuid,
            unionId: params.user ? params.user.unionid : null,
            user: params.user ? JSON.stringify(params.user) : null,
            appVer: '2.2.0'
        }, {
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        }
    );
}

export function mobilelogin(params) {
    let request = createRequest();
    return request.post(
        REQUEST_MOBILE_LOGIN,
        params, {
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        }
    );
}

export function logout(params) {
    let request = createRequest();
    return request.get(REQUEST_LOGOUT, {
        type: params.mobile ? '' : 'wx_app',
        uid: params.uid,
        token: params.token,
        appVer: '2.2.0'
    });
}
export function fetchUser(params) {
    let request = createRequest();
    return request.get(REQUEST_USER, {
        uid: params.uid,
        pageNum: params.pageNum
    });
}
export function getUserList(params) {
    let request = createRequest();
    return request.get(REQUEST_USER, {
        uid: params.uid,
        pageNum: params.pageNum
    });
}
export function addFavorite(params) {
    let request = createRequest();
    return request.get(REQUEST_LIKE_COOK, {
        uid: params.uid,
        token: params.token,
        type: 'cook',
        id: params.id,
        deviceId: params.uuid,
        appVer: '2.2.0'
    });
}

export function getMyLikesCook(params) {
    let request = createRequest();
    return request.get(REQUEST_MY_LIKES_COOK, {
        uid: params.uid,
        token: params.token,
        deviceId: params.uuid,
        appVer: '2.2.0'
    });
}

export function getMyInfo(params) {
    let request = createRequest();
    return request.get(REQUEST_MYINFO, params);
}

export function getSessionKey(params) {
    let request = createRequest();
    return request.get(REQUEST_SESSION, {
        code: params.code
    });
}

export function checkTokenAvail(status) {
    if (status == 10001) {
        return false;
    } else {
        return true;
    }
}

export function getCookEditInfo(params) {
    let request = createRequest();
    return request.get(REQUEST_COOKEDIT, params);
}

export function deleteCookDraft(params) {
    let request = createRequest();
    return request.get(REQUEST_DELETECOOKDRAFT, {
        ...params,
        appVersion: '2.2.0'
    });
}